﻿// ==========================================================================
// ApplicationSamples.cs
// Silverlight Shapes Library
// ==========================================================================
// Copyright (c) Silverlight Shapes Development Group
// All rights reserved.
// ==========================================================================

using System;
using System.IO;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Resources;

namespace SD.Samples.Configuration
{
    /// <summary>
    /// Provides access to the samples of this application.
    /// </summary>
    public static class ApplicationSamples
    {
        private static object _lockObject = new object();
        private static SampleRepository _repository;
        /// <summary>
        /// Gets the default sample definitions of this application.
        /// </summary>
        /// <value>The default sample definitions of this application.</value>
        public static SampleRepository Default
        {
            get
            {
                // Lock the object to provide single access to the sample definitions.
                lock (_lockObject)
                {
                    // Only load the applications one time.
                    if (_repository == null)
                    {
                        StreamResourceInfo resource =
                            Application.GetResourceStream(new Uri("SD.Samples;component/Configuration/SamplesConfiguration.xaml", UriKind.RelativeOrAbsolute));

                        Stream stream = resource.Stream;

                        try
                        {
                            using (StreamReader reader = new StreamReader(stream))
                            {
                                stream = null;

                                string xaml = reader.ReadToEnd();

                                // Load the sample configuration using the xaml reader.
                                _repository = (SampleRepository)XamlReader.Load(xaml);
                            }
                        }
                        finally
                        {
                            if (stream != null)
                            {
                                stream.Dispose();
                            }
                        }
                    }
                }

                return _repository;
            }
        }
    }
}
